<script setup lang="ts">
import NewCard from '@/components/NewCard.vue'
import { VUE_APP_BASE_API } from '@/utils/baseConfig'
import { onMounted, ref } from 'vue'
import { get } from '@/utils/service'

const loading = ref(false)
const finished = ref(false)
const noEmpty = ref(false)
const paramCType = localStorage.getItem('corporateCultureTypeId')
const active = ref(paramCType + '')
const newLists = ref([])
const tags = ref([])
const page = ref(1)
const corporateCultureTypeId = ref(paramCType)
const limit = 5
onMounted(() => {})
renderTag()
function onLoad() {
  console.log('onLoad')
  queryData()
}
async function renderTag() {
  get('/corporateCultureWechatNew/queryWechatTagList', {}).then((res) => {
    if (res.code === 0) {
      tags.value = res.data
      active.value = paramCType || '0'
    } else {
      console.log(res)
    }
  })
}
function onClickTab(valueObj) {
  corporateCultureTypeId.value = valueObj.name
  newLists.value = []
  page.value = 1
  loading.value = false
  finished.value = false
  noEmpty.value = false
  localStorage.setItem('corporateCultureTypeId', corporateCultureTypeId.value)
}
function queryData() {
  console.log('queryData')
  get('/corporateCultureWechatNew/queryWechatNewsList', {
    limit: limit,
    page: page.value,
    corporateCultureTypeId: corporateCultureTypeId.value,
  })
    .then((res) => {
      if (res.code === 0) {
        // 正常返回数据
        loading.value = false
        noEmpty.value = false
        res.data.forEach((item) => {
          newLists.value.push({
            ...item,
            picture: VUE_APP_BASE_API + item.picture,
          })
        })
        page.value++
      } else {
        //其他返回数据情况
        if (res.count > 0) {
          //有数据情况
          if (res.code == 201) {
            loading.value = true
            finished.value = true
            noEmpty.value = false
          }
        } else {
          //无数据情况
          loading.value = true
          finished.value = true
          noEmpty.value = true
        }
      }
    })
    .catch((err) => {
      console.log(err)
    })
}
</script>
<template>
  <div>
    <van-image
      width="100%"
      height="auto"
      :src="VUE_APP_BASE_API + '/images/vue/rewardBanner.jpg'"
    />
    <van-tabs v-model:active="active" @click-tab="onClickTab" color="var(--main-color)">
      <van-tab v-for="tag in tags" :title="tag.name" :key="tag.dbid" :name="tag.dbid"></van-tab>
    </van-tabs>
    <van-empty
      v-if="noEmpty == true"
      :image="VUE_APP_BASE_API + '/images/vue/noData.png'"
      image-size="100"
      description="暂无数据"
    />
    <van-list
      v-else
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <NewCard
        :item="newItem"
        v-for="newItem in newLists"
        :show-tile-state="false"
        :key="newItem.dbid"
      ></NewCard>
      <br />
    </van-list>
  </div>
  <br />
  <br />
</template>
<style></style>
